//
//  YBCodeController.m
//  Cocoachina
//
//  Created by 游兵 on 2017/9/15.
//  Copyright © 2017年 YBing. All rights reserved.
//

#import "YBCodeController.h"
#import "YBCodeMenuController.h"
#import "YBCodeShowController.h"

#ifndef kYBCodeMenuWidth 
#define kYBCodeMenuWidth (kScreenW*3/4.0)
#endif

@interface YBCodeController ()

@property (nonatomic, strong) YBCodeMenuController *menuVC;
@property (nonatomic, strong) YBCodeShowController *showVC;

@property (nonatomic, strong) UIView *bgView;

@end

@implementation YBCodeController

- (UIView *)bgView
{
    if (_bgView==nil) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];
        [_bgView addGestureRecognizer:tap];
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [_bgView addGestureRecognizer:pan];
    }
    return _bgView;
}

- (YBCodeMenuController *)menuVC
{
    if (_menuVC==nil) {
        _menuVC = [YBCodeMenuController new];
        
        _menuVC.view.frame = CGRectMake(-kYBCodeMenuWidth, 20, kYBCodeMenuWidth, kScreenH-20);
        YBWeakSelf
        [_menuVC setDidSelectedBlock:^(YBCodeTag *tag){
            [weakSelf close];
            weakSelf.showVC.url = [[NSString stringWithFormat:@"http://code.cocoachina.com%@", tag.url] stringByDeletingLastPathComponent];
            weakSelf.navigationItem.title = tag.zh;
        }];
    }
    return _menuVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationBar];
    [self setupShowVC];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
}

- (void)setupShowVC
{
    self.showVC = [YBCodeShowController new];
    YBWeakSelf
    [self.showVC setGetDataBlock:^(TFHpple *doc){
        weakSelf.menuVC.doc = doc;
        weakSelf.showVC.getDataBlock = nil;
    }];
    self.showVC.url = @"http://code.cocoachina.com/";
    [self addChildViewController:self.showVC];
    self.showVC.view.frame = CGRectMake(0, 0, kScreenW, kScreenH-49);
    [self.view addSubview:self.showVC.view];
}

- (void)setupNavigationBar
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithText:@"\U0000e623" color:[UIColor whiteColor] size:CGSizeMake(22, 22)] style:UIBarButtonItemStylePlain target:self action:@selector(menu)];
}

- (void)menu
{
    [self show];
}

- (void)pan:(UIPanGestureRecognizer *)pan
{
    UIGestureRecognizerState state = pan.state;
    if (state==UIGestureRecognizerStateBegan) {
        if (pan.view == self.bgView) return;
        CGPoint startPoint = [pan locationInView:pan.view];
        if (startPoint.x>64) return;
        [self show_pre];
    } else {
        if (pan.view==self.view && self.menuVC.view.superview==nil) return;
        if (state==UIGestureRecognizerStateEnded) {
            if (CGRectGetMaxX(self.menuVC.view.frame)>kYBCodeMenuWidth/2.0) {
                [UIView animateWithDuration:0.1 animations:^{
                    self.menuVC.view.x = 0;
                }];
            } else {
                [UIView animateWithDuration:0.1 animations:^{
                    self.menuVC.view.x = -kYBCodeMenuWidth;
                } completion:^(BOOL finished) {
                    [self closeCompletion];
                }];
            }
            
        } else {
            
            CGPoint point = [pan translationInView:pan.view];
            [pan setTranslation:CGPointZero inView:pan.view];
            self.menuVC.view.x += point.x;
            if (self.menuVC.view.x>0) {
                self.menuVC.view.x = 0;
            }
            self.bgView.alpha = 0.4*(kYBCodeMenuWidth+self.menuVC.view.x)/kYBCodeMenuWidth;
        }
    }
}

- (void)show
{
    [self show_pre];
    [UIView animateWithDuration:0.3 animations:^{
        self.menuVC.view.x = 0;
        self.bgView.alpha = 0.4;
    }];
}

- (void)show_pre
{
    [self.tabBarController.view addSubview:self.bgView];
    [self.tabBarController addChildViewController:self.menuVC];
    [self.tabBarController.view addSubview:self.menuVC.view];
}

- (void)close
{
    [UIView animateWithDuration:0.3 animations:^{
        self.menuVC.view.x = -kScreenW/2.0;
        self.bgView.alpha = 0;
    } completion:^(BOOL finished) {
        [self closeCompletion];
    }];
}

- (void)closeCompletion
{
    [self.bgView removeFromSuperview];
    [self.menuVC.view removeFromSuperview];
    [self.menuVC removeFromParentViewController];
}


@end
